package br.com.locanet.view.impl;

import javax.faces.bean.ManagedBean;

import br.com.locanet.bean.impl.Funcionario;
import br.com.locanet.exception.InvalidAttributeException;
import br.com.locanet.service.impl.FuncionarioService;
import br.com.locanet.view.AbstractManagedBean;

@ManagedBean(name="funcionarioBean")
public class FuncionariosBean extends AbstractManagedBean<Funcionario,FuncionarioService> {
	public String insert() throws Exception {
		getEntity().setStatus(1);
		getEntityService().doValidateSenha(getEntity(), getRequest().getParameter("conf_senha"));
		return super.insert();
	}
	
	public void delete() throws Exception {
		doExclusaoLogica(getEntity());
		select();
	}
	
	private void doExclusaoLogica(Funcionario func) throws Exception{
		func = getEntityService().selectById(func);
		func.setStatus(func.getStatus()==1?0:1);
		getEntityService().update(func);
	}

	public void select() throws Exception {
		getEntityService().select(getEntity());
	}
	
	public void get_update() throws Exception{
		if (getEntity().getId()==null) throw new InvalidAttributeException("idFuncionarioNaoInformado");
		
		Funcionario funcionario = getEntityService().selectById(getEntity());
		
		getRequest().setAttribute("funcionario", funcionario);
	}
}
